package com.agricultural.machine.factory;

import com.agricultural.machine.common.AbstractMachine;
import com.agricultural.machine.environment.EnvironmentData;
import com.agricultural.machine.messaging.Message;
import com.agricultural.machine.messaging.MessageBroker;

/**
 * 播种机类
 */
public class SeederMachine extends AbstractMachine {
    private int seedingDepth; // 播种深度 (厘米)
    private String seedType;  // 种子类型
    
    public SeederMachine() {
        super("seeder");
        this.seedingDepth = 5; // 默认播种深度5厘米
        this.seedType = "小麦";
    }
    
    @Override
    public void work() {
        if (isRunning()) {
            System.out.println("播种机 " + getMachineId() + " 正在播种作业...");
            System.out.println("当前播种深度: " + seedingDepth + "厘米, 种子类型: " + seedType);
        } else {
            System.out.println("播种机 " + getMachineId() + " 未启动，无法工作");
        }
    }
    
    /**
     * 设置播种深度
     */
    public void setSeedingDepth(int depth) {
        if (depth < 1) {
            depth = 1;
        } else if (depth > 10) {
            depth = 10;
        }
        
        this.seedingDepth = depth;
        System.out.println("播种机 " + getMachineId() + " 已调整播种深度为 " + depth + " 厘米");
    }
    
    /**
     * 设置种子类型
     */
    public void setSeedType(String seedType) {
        this.seedType = seedType;
        System.out.println("播种机 " + getMachineId() + " 已更换种子类型为 " + seedType);
    }
    
    /**
     * 获取播种深度
     */
    public int getSeedingDepth() {
        return seedingDepth;
    }
    
    /**
     * 获取种子类型
     */
    public String getSeedType() {
        return seedType;
    }
    
    /**
     * 根据环境数据自动调整播种操作
     */
    @Override
    protected void automaticAdjustOperation(EnvironmentData environmentData) {
        if (!isRunning()) {
            return;
        }
        
        // 检查是否适合播种
        if (!environmentData.isSuitableForSeeding()) {
            // 不适合播种，发送消息并停止
            System.out.println("播种机 " + getMachineId() + " 检测到不适合播种的环境条件，暂停作业");
            stop();
            
            // 通知拖拉机停止前进
            notifyTractorToStop("环境条件不适合播种，请停止前进");
            return;
        }
        
        // 根据土壤湿度调整播种深度
        double soilMoisture = environmentData.getSoilMoisture();
        int newDepth;
        
        if (soilMoisture > 70) {
            // 土壤过湿，减少播种深度
            newDepth = 3;
            System.out.println("播种机 " + getMachineId() + " 检测到土壤湿度较高 (" + soilMoisture + "%)，自动减少播种深度");
        } else if (soilMoisture < 40) {
            // 土壤过干，增加播种深度
            newDepth = 7;
            System.out.println("播种机 " + getMachineId() + " 检测到土壤湿度较低 (" + soilMoisture + "%)，自动增加播种深度");
        } else {
            // 土壤湿度适中，使用标准深度
            newDepth = 5;
        }
        
        // 只有当需要改变深度时才设置
        if (newDepth != seedingDepth) {
            setSeedingDepth(newDepth);
        }
    }
    
    /**
     * 通知拖拉机停止
     */
    private void notifyTractorToStop(String reason) {
        Message message = new Message(
                getMachineId(),
                "broadcast",
                "tractor_stop_request",
                reason
        );
        MessageBroker.getInstance().sendMessage(message);
    }
}












